from django.shortcuts import render
from django.http import HttpResponseRedirect,JsonResponse,HttpResponse

from Lion.database import *
from datetime import timedelta,datetime
from random import randint
from .tasks import test_task

# Create your views here.
def redis_api(request,id):
    for i in rediscluster:
        if i.get('ID') == id:
            return JsonResponse(i.get("ClusterHost"),safe=False)
    return JsonResponse({"status":500,"message":"没有找到数据"})


def index_chart(request):
    n = 50
    dataProvider = [0]*n
    
    now = datetime.now()
    for i in range(0,n):
        x = {}
        x["column-1"] = randint(1000,2000)
        x["column-2"] = randint(100,1000)
        x["column-3"] = randint(50,400)
        #x["date"] = (now - timedelta(seconds =(n-i)*3600)).strftime("%Y-%m-%d %H:%M")
        x["date"] = (now - timedelta(hours =(n-i))).strftime("%Y-%m-%d %H")
        dataProvider[i] = x

    return JsonResponse(dataProvider,safe=False)

def test(request):
    x = test_task.delay()
    return JsonResponse({'task_id':x.task_id,})